// UserMapper.java  
package cn.edu.zust.se.dao;
import cn.edu.zust.se.model.Concern;
import cn.edu.zust.se.model.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface UserMapper {
    // 用户增删改查  
    @Insert("INSERT INTO user(username, password, name, intro, gender, province, city, regist_time, type, email, mobile, QQ, status) " +
            "VALUES(#{username}, #{password}, #{name}, #{intro}, #{gender}, #{province}, #{city}, #{registTime}, #{type}, #{email}, #{mobile}, #{QQ}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addUser(User user);

    @Update("UPDATE user SET username=#{username}, password=#{password}, name=#{name}, intro=#{intro} WHERE id=#{id}")
    void updateUser(User user);

    @Delete("DELETE FROM user WHERE id=#{id}")
    void deleteUser(Integer id);

    @Select("SELECT * FROM user")
    List<User> listAllUsers();

    @Select("SELECT * FROM user WHERE username = '${username}'")
    List<User> unsafeFindByUsername(@Param("username") String username);

    // 关注功能  
    @Insert("INSERT INTO concern(concerner_id, concerned_id, concern_time) VALUES(#{concernerId}, #{concernedId}, #{concernTime})")
    void addConcern(Concern concern);

    @Delete("DELETE FROM concern WHERE concerner_id=#{concernerId} AND concerned_id=#{concernedId}")
    void deleteConcern(@Param("concernerId") Integer concernerId, @Param("concernedId") Integer concernedId);

    @Select("SELECT u.* FROM user u JOIN concern c ON u.id = c.concerner_id WHERE c.concerned_id=#{userId}")
    List<User> getFans(Integer userId);

    @Select("SELECT u.* FROM user u JOIN concern c ON u.id = c.concerned_id WHERE c.concerner_id=#{userId}")
    List<User> getFollowedUsers(Integer userId);
}  
